var config = require('config.js');
var common = require('common.js');
var db = require('db.js');
//需要token才能访问的数组
var methodToken = [
	'apartment.getMgrOperatorsInfo',
	'apartment.getMgrDormInfo',
	'apartment.getSuppliesInfo',
	'apartment.getSuppliesInfoEx',
	'apartment.getCollegeSuppliesInfo',
	'apartment.getCollegeSuppliesDetail',
	'apartment.getDormInfo',
	'apartment.getSuppliesStatus',
	'apartment.setSuppliesStatus', 
	'apartment.exportDailyData',
	'apartment.exportCollegeData',
	'apartment.suppliesDetail',
	'book.getYears',
	'book.getSeasons',
	'book.getDepts',
	'book.getSpecicalties',
	'book.getClasses',
	'book.getDirs',
	'book.getTeachers',
	'book.getApplyClassList',
	'book.getApplyLogList',
	'book.getBookAplyDetail',
	'book.getBookAplyLogDetail',
	'book.studentlock',
	'book.studentunlock',
	'book.bookApply',
	'book.getStudentInfo',
	'book.bookDraw',
	'book.getBookMgrOperatorsInfo',
	'book.getBookDrawInfo',
	'book.getDrawClassList',
	'book.getDrawConfirmDetail',
	'book.bookListDraw',
	'book.bookDetailDraw',
	'book.changeStudent',
	'book.exportExcel',
	'user.menuInfo',
	'user.logout',
	'user.info',
	'user.editpwd',
	'student.getChangeMajorStudentList',
	'student.getChangeMajorStudentDetail',
	'student.changeMajorStudentConfirmList',
	'student.changeMajorStudentConfirmDetail',
	'student.changeTeacher',
	'student.getWrongStudent',
	'student.receive',
];

//接口统一封装
function api(method, data, callback, show = true) {
	//如果是需要登录的，增加token
	if (methodToken.indexOf(method) >= 0) {
		var userToken = db.get('userToken');
		if (!userToken) {
			common.jumpToLogin();
		} else {
			data.token = userToken;
			data.method = method;
			post(data, callback, show);
		}
	} else {
		data.method = method;
		post(data, callback, show);
	}
}

//post请求
function post(data, callback, show) {
	if (show) {
		wx.showLoading({
			title: '载入中...'
		});
	}
	wx.request({
		url: config.api_url + 'api.html',
		data: data,
		method: 'POST',
		success: function(res) {
			if (show) {
				wx.hideLoading();
			}
			//这里做判断，如果不报错就返回，如果报错，就做错误处理
			if (res.data.status) {
				callback(res.data);
			} else {
				error(res.data, callback, data);
			}
		},
		fail: function(res) {
			if (show) {
				wx.hideLoading();
			}
			return {
				status: false,
				data: res.data,
				msg: '接口调用失败',
			};
		},
		complete: function(res) {

		}
	});
}

//post直接请求
function post2(path, data, callback, show = true) {
	if (show) {
		wx.showLoading({
			title: '载入中...'
		});
	}
	wx.request({
		url: config.api_url + 'api/' + path,
		data: data,
		method: 'POST',
		success: function(res) {
			if (show) {
				wx.hideLoading();
			}
			//这里做判断，如果不报错就返回，如果报错，就做错误处理
			if (res.data.status) {
				callback(res.data);
			} else {
				error(res.data, callback);
			}
		},
		fail: function(res) {
			if (show) {
				wx.hideLoading();
			}
			return {
				status: false,
				data: res.data,
				msg: '接口调用失败',
			};
		},
		complete: function(res) {

		}
	});
}

//接口错误信息处理
function error(res, callback, postData) {
	switch (res.data) {
		case 14007: //token验证失败，需要从新登录
			//判断是否是需要登录的接口
			if (methodToken.indexOf(postData.method) >= 0) {
				wx.removeStorageSync('jump_to_login');
				wx.removeStorageSync('userToken');
				wx.removeStorageSync('myInviteCode');
				common.jumpToLogin();
			}
			break;
		case 2:
			;
			break;
		default:
			callback(res);
	}
}

/**
 * 新的微信登录方法
 * data的值报错
 *   mobile:手机号码
 *   code:手机短信验证码，可为空
 *  wx_code: 
 *   wx_iv: 
 *   wx_edata: 
 * 
 */
function login1(data, callback) {
	api('user.wxapplogin1', data, function(res2) {
		//res2.wxUserInfo = res1.userInfo;    //微信的用户信息保存起来
		callback(res2);
	});
}

function login2(data, callback) {
	//加入邀请码
	let pid = db.get('invitecode');
	if (pid) {
		data['pid'] = pid;
	}
	api('user.wxapplogin2', data, function(res2) {
		//res2.wxUserInfo = res1.userInfo;    //微信的用户信息保存起来
		callback(res2);
	});
}

//账号绑定
function wxLogin(data, callback) {
	data.platform = 2; // 小程序登录
	api('user.wxLogin', data, function(res2) {
		callback(res2);
	});
}

//账号解绑
function logout(data, callback) {
	data.platform = 2; // 小程序登录
	api('user.logout', data, function(res2) {
		callback(res2);
	});
}

//手机号码短信登录
function smsLogin(data, callback) {
	data.platform = 2; //手机号码必须制定登录平台，否则，取到的token可能不是微信小程序平台的
	api('user.smslogin', data, function(res2) {
		callback(res2);
	});
}

//手机号码短信登录
function accountFreeze(data, callback) {
	data.platform = 2; //手机号码必须制定登录平台，否则，取到的token可能不是微信小程序平台的
	api('user.accountfreeze', data, function(res2) {
		callback(res2);
	});
}

//发送手机短信验证码
function sms(mobile, code, callback) {
	//code = 2是登录，等1是注册,默认等于2
	var data = {
		mobile: mobile,
		code: code
	};
	api('user.sms', data, function(res2) {
		callback(res2);
	});
}

//账号登录
function editpwd(data, callback) {
	data.platform = 2; // 小程序登录
	api('user.editpwd', data, function(res2) {
		callback(res2);
	});
}

//重置密码
function forgotPwd(data, callback) {
	data.platform = 2; // 小程序登录
	api('user.forgotpwd', data, function(res2) {
		callback(res2);
	});
}

//用户信息接口
function userInfo(callback) {
	api('user.info', {}, function(res) {
		callback(res);
	}, false);
}

//获取用户授权的菜单信息
function menuInfo(callback) {
	api('user.menuInfo', {}, function(res) {
		callback(res);
	}, false);
}

//广告接口
function adList(code, callback) {
	var data = {
		code: code
	};
	api('advert.getAdvertList', data, function(res) {
		callback(res);
	});
}

//公告接口
function noticeList(callback) {
	api('notice.noticeList', {}, function(res) {
		callback(res);
	}, false);
}
//获取公告内容
function noticeInfo(data, callback) {
	api('notice.noticeInfo', data, function(res) {
		callback(res);
	});
}

// 获取当前操作员信息
function getApartmentOperatorsInfo(data, callback) {
	data.platform = 2; // 小程序登录
	api('user.info', data, function(res2) {
		callback(res2);
	});
}

//获取学生公寓用品领用信息
function getApartmentSupplies(data, callback) {
	data.platform = 2; // 小程序登录
	api('apartment.getSuppliesStatus', data, function(res2) {
		callback(res2);
	});
}

//获取学生公寓用品领用信息
function setApartmentSupplies(data, callback) {
	data.platform = 2; // 小程序登录
	api('apartment.setSuppliesStatus', data, function(res2) {
		callback(res2);
	});
}

// 获取当前操作员所负责的宿舍楼
function getApartmentDormInfo(data, callback) {
	data.platform = 2; // 小程序登录
	api('apartment.getDormInfo', data, function(res2) {
		callback(res2);
	});
}

// 获取当前操作员所负责的宿舍楼
function getApartmentMgrDormInfo(data, callback) {
	data.platform = 2; // 小程序登录
	api('apartment.getMgrDormInfo', data, function(res2) {
		callback(res2);
	});
}

// 获取学院管理员负责的所有操作员信息
function getApartmentMgrOperatorsInfo(data, callback) {
	data.platform = 2; // 小程序登录
	api('apartment.getMgrOperatorsInfo', data, function(res2) {
		callback(res2);
	});
}

// 根据宿舍楼、管理员、时间获取物品发放清单
function getApartmentSuppliesInfo(data, callback) {
	data.platform = 2; // 小程序登录
	api('apartment.getSuppliesInfo', data, function(res2) {
		callback(res2);
	});
}

// 获取物品发放清单
function getApartmentSuppliesInfoEx(callback) {
	api('apartment.getSuppliesInfoEx', {}, function(res2) {
		callback(res2);
	});
}

// 根据学院获取物品发放清单
function getCollegeSuppliesInfo(callback) {
	api('apartment.getCollegeSuppliesInfo', {}, function(res2) {
		callback(res2);
	});
}

// 根据学院获取物品发放明细
function getCollegeSuppliesDetail(data, callback) {
	api('apartment.getCollegeSuppliesDetail', data, function(res2) {
		callback(res2);
	});
}

// 获取物品发放清单
function apartmentExportDailyData(data, callback) {
	data.platform = 2; // 小程序登录
	api('apartment.exportDailyData', data, function(res2) {
		callback(res2);
	});
}

// 根据时间获取物品发放清单
function getApartmentSuppliesDetail(data, callback) {
	data.platform = 2; // 小程序登录
	api('apartment.suppliesDetail', data, function(res2) {
		callback(res2);
	});
}

// 根据学院导出物品发放清单
function apartmentExportCollegeData(data, callback) {
	data.platform = 2; // 小程序登录
	api('apartment.exportCollegeData', data, function(res2) {
		callback(res2);
	});
}

// 获取年份
function getYears(callback) {
	api('book.getYears', {} , function(res2) {
		callback(res2);
	});
}

// 获取季度
function getSeasons(callback) {
	api('book.getSeasons', {} , function(res2) {
		callback(res2);
	});
}

// 获取部门
function getDepts(callback) {
	api('book.getDepts', {} , function(res2) {
		callback(res2);
	});
}


// 获取专业
function getSpecicalties(callback) {
	api('book.getSpecicalties', {} , function(res2) {
		callback(res2);
	});
}


// 获取班级
function getClasses(data, callback) {
	api('book.getClasses', data, function(res2) {
		callback(res2);
	});
}


// 获取方向
function getDirs(callback) {
	api('book.getDirs', {}, function(res2) {
		callback(res2);
	});
}


// 获取辅导员
function getTeachers(callback) {
	api('book.getTeachers', {}, function(res2) {
		callback(res2);
	});
}
 
// 获取申请班级列表
function getApplyClassList(data, callback) {
	api('book.getApplyClassList', data, function(res2) {
		callback(res2);
	});
}

// 获取申请班级列表
function getApplyLogList(data, callback) {
	api('book.getApplyLogList', data, function(res2) {
		callback(res2);
	});
}

// 获取申请班级明细
function getBookAplyDetail(data, callback) {
	api('book.getBookAplyDetail', data, function(res2) {
		callback(res2);
	});
}

// 获取申请记录班级明细
function getBookAplyLogDetail(data, callback) {
	api('book.getBookAplyLogDetail', data, function(res2) {
		callback(res2);
	});
}

// 锁定学生
function studentlock(data, callback) {
	api('book.studentlock', data, function(res2) {
		callback(res2);
	});
}

// 解锁学生
function studentunlock(data, callback) {
	api('book.studentunlock', data, function(res2) {
		callback(res2);
	});
}

// 提交请求
function bookApply(data, callback) {
	api('book.bookApply', data, function(res2) {
		callback(res2);
	});
}

// 提交请求
function getStudentInfo(data, callback) {
	api('book.getStudentInfo', data, function(res2) {
		callback(res2);
	});
}

// 教材发放
function bookDraw(data, callback) {
	api('book.bookDraw', data, function(res2) {
		callback(res2);
	});
}

// 查询教材发放员
function getBookMgrOperatorsInfo(data, callback) {
	api('book.getBookMgrOperatorsInfo', data, function(res2) {
		callback(res2);
	});
}

// 查询教材每日发放记录
function getBookDrawInfo(data, callback) {
	api('book.getBookDrawInfo', data, function(res2) {
		callback(res2);
	});
}

// 获取签收班级列表
function getDrawClassList(data, callback) {
	api('book.getDrawClassList', data, function(res2) {
		callback(res2);
	});
}

// 获取签收班级明细
function getDrawConfirmDetail(data, callback) {
	api('book.getDrawConfirmDetail', data, function(res2) {
		callback(res2);
	});
}

// 班级批量签收
function bookListDraw(data, callback) {
	api('book.bookListDraw', data, function(res2) {
		callback(res2);
	});
}

// 个人批量签收
function bookDetailDraw(data, callback) {
	api('book.bookDetailDraw', data, function(res2) {
		callback(res2);
	});
}

// 转让教材
function changeStudent(data, callback) {
	api('book.changeStudent', data, function(res2) {
		callback(res2);
	});
}

// 导出签收记录
function exportExcel(data, callback) {
	api('book.exportExcel', data, function(res2) {
		callback(res2);
	});
}

// 获取转专业学生班级清单
function getChangeMajorStudentList(data, callback) {
	api('student.getChangeMajorStudentList', data, function(res2) {
		callback(res2);
	});
}

// 获取转专业学生明细
function getChangeMajorStudentDetail(data, callback) {
	api('student.getChangeMajorStudentDetail', data, function(res2) {
		callback(res2);
	});
}

// 班级批量确认转专业
function changeMajorStudentConfirmList(data, callback) {
	api('student.changeMajorStudentConfirmList', data, function(res2) {
		callback(res2);
	});
}

// 个人确认转专业
function changeMajorStudentConfirmDetail(data, callback) {
	api('student.changeMajorStudentConfirmDetail', data, function(res2) {
		callback(res2);
	});
}

// 更换辅导员
function changeTeacher(data, callback) {
	api('student.changeTeacher', data, function(res2) {
		callback(res2);
	});
}

// 更换辅导员
function getWrongStudent(callback) {
	api('student.getWrongStudent', {}, function(res2) {
		callback(res2);
	});
}

// 更换辅导员
function receive(data, callback) {
	api('student.receive', data, function(res2) {
		callback(res2);
	});
}

module.exports = {
	login1: login1,
	login2: login2,
	smsLogin: smsLogin,
	accountFreeze: accountFreeze,
	wxLogin: wxLogin,
	logout: logout, 
	editpwd: editpwd,
	forgotPwd: forgotPwd,
	sms: sms,
	userInfo: userInfo,
	adList: adList,
	noticeList: noticeList,
	noticeInfo: noticeInfo,
	menuInfo: menuInfo,
	getApartmentSupplies: getApartmentSupplies,
	setApartmentSupplies: setApartmentSupplies,
	getApartmentDormInfo: getApartmentDormInfo,
	getApartmentOperatorsInfo: getApartmentOperatorsInfo,
	getApartmentSuppliesInfo: getApartmentSuppliesInfo,
	getApartmentSuppliesInfoEx: getApartmentSuppliesInfoEx,
	getCollegeSuppliesInfo: getCollegeSuppliesInfo,
	getCollegeSuppliesDetail: getCollegeSuppliesDetail,
	getApartmentSuppliesDetail: getApartmentSuppliesDetail,
	apartmentExportDailyData: apartmentExportDailyData,
	apartmentExportCollegeData: apartmentExportCollegeData,
	getApartmentMgrDormInfo: getApartmentMgrDormInfo,
	getApartmentMgrOperatorsInfo: getApartmentMgrOperatorsInfo,
	getYears: getYears,
	getSeasons: getSeasons,
	getDepts: getDepts,
	getSpecicalties: getSpecicalties,
	getClasses: getClasses,
	getDirs: getDirs,
	getTeachers: getTeachers,
	getApplyClassList: getApplyClassList,
	getApplyLogList: getApplyLogList,
	getBookAplyDetail: getBookAplyDetail,
	getBookAplyLogDetail: getBookAplyLogDetail,
	studentlock: studentlock,
	studentunlock: studentunlock,
	bookApply: bookApply,
	getStudentInfo: getStudentInfo,
	bookDraw: bookDraw,
	getBookMgrOperatorsInfo: getBookMgrOperatorsInfo,
	getBookDrawInfo: getBookDrawInfo,
	getDrawClassList: getDrawClassList,
	getDrawConfirmDetail: getDrawConfirmDetail,
	bookListDraw: bookListDraw,
	bookDetailDraw: bookDetailDraw,
	changeStudent: changeStudent,
	exportExcel: exportExcel,
	getChangeMajorStudentList:getChangeMajorStudentList,
	getChangeMajorStudentDetail:getChangeMajorStudentDetail,
	changeMajorStudentConfirmList:changeMajorStudentConfirmList,
	changeMajorStudentConfirmDetail:changeMajorStudentConfirmDetail,
	changeTeacher:changeTeacher,
	getWrongStudent:getWrongStudent,
	receive:receive
}
